/*
*
* sprite.cpp
* A simple but useful class that provides a bitmap and its coordinates
*
*/
#include "sprite.h"


/*  Constructor 1
*   @arg:
*     - const char * spr_path : The absolute path to the image
*/
Sprite::Sprite(const char * spr_path) {
  _sprite = load_bitmap(spr_path, NULL);
  _x = 0;
  _y = 0;
  _hidden = 0;
}

/*  Constructor 2
*
*/
Sprite::Sprite() {
  _sprite = NULL;
  _x = 0;
  _y = 0;
  _hidden = 0;
}

/*  __getSprite() : returns the Bitmap pointer
*
*/
BITMAP * Sprite::__getSprite() {
  if ( _hidden == 1 ) return 0x00;
  return _sprite;
}

/*  __setBitmap() : change the BITMAP pointed by _sprite
*   @arg:
*     - BITMAP * nSprite : [must be defined] BITMAP resource pointer
*/
void Sprite::__setBitmap(BITMAP * nBMP) {
  _sprite = nBMP;
}

/*  __moveX()/__moveY() : moves horizontally/vertically the sprite position
*   @arg:
*     - int val : Length of movement (in pixels)
*/
void Sprite::__moveX(int val) { _x += val; }
void Sprite::__moveY(int val) { _y += val; }

/*  __move() : moves both coordinates of the sprite
*   @arg:
*     - int xVal : X distance
*     - int yVal : Y distance
*/
void Sprite::__move(int xVal, int yVal) {
 _x += xVal;
 _y += yVal;
}

/*  __setX()/__setY() : set the X/Y position
*   @arg:
*     - int val : New position
*/
void Sprite::__setX(int val) { _x = val; }
void Sprite::__setY(int val) { _y = val; }

/*  __getX() : Returns the X value of the Sprite
*   __getY() : Returns the Y value
*/
int Sprite::__getX() { return _x; }
int Sprite::__getY() { return _y;  }

/*  __collidesWith() : return true if the sprite collides with another given Sprite
*   @arg:
*     - Sprite * s : The sprite that needs to be checked (return FALSE if either one or the other sprite has its bounding box disabled)
*/
bool Sprite::__collidesWith(Sprite * s) {

  if ( _bbs == 0 || s->_bbs == 0 ) return false;
  // Contains all of the x positions
  int xArr[4] = { _bbx1 + _x, _bbx2 + _x, s->_bbx1 + s->_x, s->_bbx2 + s->_x };
  // Contains all of the y positions
  int yArr[4] = { _bby1 + _y, _bby2 + _y, s->_bby1 + s->_y, s->_bby2 + s->_y };

  if ( xArr[1] < xArr[2] ) return false;
  if ( xArr[0] > xArr[3] ) return false;
  if ( yArr[1] < yArr[2] ) return false;
  if ( yArr[0] > yArr[3] ) return false;
  return true;
}

/* __hide()
*
*/
void Sprite::__hide(int s) { _hidden = (s == 1)? 1 : 0; }
